﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace myChannel9
{
    public partial class Control_Twitter : UserControl
    {
        public Control_Twitter()
        {
            InitializeComponent();

            loadTwitter();
        }

        public void loadTwitter ()
        {
            lblLoading.Visibility = Visibility.Visible;
            lboTwitter.Visibility = Visibility.Collapsed;

            WebClient client = new WebClient();
            client.DownloadStringAsync(new Uri("http://search.twitter.com/search.atom?q=channel9"));
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
        }

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                List<TwitterObject> twitteList = new List<TwitterObject>();

                XDocument xml = XDocument.Parse(e.Result);
                XNamespace atomNS = "http://www.w3.org/2005/Atom";
    
                var tempList = (from entry in xml.Descendants(atomNS + "entry")
                                select new TwitterObject()
                                {
                                    ID = entry.Element(atomNS + "id").Value,
                                    Title = entry.Element(atomNS + "title").Value,
                                    Date = DateTime.Parse(entry.Element(atomNS + "published").Value),
                                    AuthorName = entry.Descendants(atomNS + "author").Elements(atomNS + "name").FirstOrDefault().Value,
                                    AuthorUri = entry.Descendants(atomNS + "author").Elements(atomNS + "uri").FirstOrDefault().Value,
                                    AuthorImage = (from imgElement in entry.Elements(atomNS + "link")
                                                   where imgElement.Attribute("rel") != null
                                                   && imgElement.Attribute("rel").Value.Contains("image")
                                                   && imgElement.Attribute("href") != null
                                                   select imgElement.Attribute("href").Value).FirstOrDefault()
                                }).ToList();
 
                twitteList = tempList.ToList();

                lboTwitter.ItemsSource = twitteList;
            }

            lblLoading.Visibility = Visibility.Collapsed;
            lboTwitter.Visibility = Visibility.Visible;
        }

        private void btnRefresh_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            loadTwitter();
        }

    }

    public class TwitterObject
    {
        public string ID { get; set; }
        public string Title { get; set; }
        public DateTime Date { get; set; }
        public string AuthorName { get; set; }
        public string AuthorUri { get; set; }
        public string AuthorImage { get; set; }
    }

}
